#region Imports
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using InsightHumans = PHSRAG.Insight21.Humans;
#endregion


namespace PHSRAG.Insight21.InsightWebControls
{
    /// <summary>
    ///  Navigation Bar for CoI Overview Pages
    /// </summary>
    [ToolboxData("<{0}:CoISubNavigationrunat=server />")]
    [Designer(typeof(CoISubNavigationDesigner), typeof(IDesigner))]
    public class CoISubNavigation : SubNavigation
    {
        #region Constants
        private const string selectedCoISubNavigationLinkKey =
            "SelectedCoISubNavigationLink_C41F1BE5-CF28-485f-8189-D289467BECD4";
        private const string SupervisorSummary = "Supervisor Summary";
        private const string HierarchyMaintenence = "Hierarchy Maintenance";
        private const string MyCOI = "My COI";
        private const string DisclosureLookUp = "Disclosure Lookup";
        private const string StandardReports = "Standard Reports";
        private const string CustomReports = "Custom Reports";
        private const string FormsManagement = "Forms Management";
        private const string AdminManagement = "COI Admin";
        #endregion

        #region Instance Variables
        private LinkButton supervisorSummaryLinkButton; //Supervisor summary
        private LinkButton hierarchyMaintenanceLinkButton; //hierarchy maintenance
        private LinkButton myCoILinkButton;
        private LinkButton disclosureLookupLinkButton;
        private LinkButton formsManagementLinkButton;
        private LinkButton standardReportLinkButton;
        private LinkButton customReportsLinkButton;
        private LinkButton cOIAdminLinkButton;

        //Making MyCOI visible.invisible will not handled(since, the least a person could have access to is MyCOI).
        private bool showSupervisorSummary = true;
        private bool showHierarchyMaintenance = true;
        private bool showDisclosureLookup = true;
        //Hiding the standard reports page (All the functionalities of this page had been transported to the custom report page)
        //Note:No matter whoever logs in, the standard report page will be unavailable.
        private bool showStandardReports;
        private bool showCustomReports;
        private bool showFormsManagement;
        private bool showAdminManagement;
        private int accessLevel;
        private bool isLoggedInUserADelegate;
        #endregion

        #region Implementations for abstract properties and methods from Base Class SubNavigation

        /// <summary>
        /// Setup the control tree to reflect the actual (and exact) layout of the controls 
        /// within the CoISubNavigation.
        /// </summary>
        protected override void GenerateControlTree()
        {
            Table table = new Table();
            Controls.Add(table);
            table.CssClass = "SubModuleSummaryNavigation";
            table.EnableViewState = false;

            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell cell = new TableCell();
            row.Cells.Add(cell);
            cell.CssClass = "SubModuleSummaryNavigationFillerCell";
            cell.Controls.Add(new LiteralControl("&nbsp;"));

            cell = new TableCell();
            row.Cells.Add(cell);

            table = new Table();
            cell.Controls.Add(table);

            row = new TableRow();
            table.Rows.Add(row);

            cell = new TableCell();
            row.Cells.Add(cell);

            cell = new TableCell();
            row.Cells.Add(cell);
            cell.Controls.Add(myCoILinkButton);

            if (showDisclosureLookup)
            {
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Controls.Add(disclosureLookupLinkButton);
            }
            if (showSupervisorSummary)
            {
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Controls.Add(supervisorSummaryLinkButton);
            }
            if (showHierarchyMaintenance)
            {
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Controls.Add(hierarchyMaintenanceLinkButton);
            }

            if (showStandardReports)
            {
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Controls.Add(standardReportLinkButton);
            }
            if (showCustomReports)
            {
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Controls.Add(customReportsLinkButton);
            }
            if (showFormsManagement)
            {
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Controls.Add(formsManagementLinkButton);
            }
            if (showAdminManagement)
            {
                cell = new TableCell();
                row.Cells.Add(cell);
                cell.Controls.Add(cOIAdminLinkButton);
            }

            CreateSelectedLinkAdornments(row);
        }

        /// <summary>
        /// The selected link cell is given a prefix and suffix image as adornments. In addition the background of the
        /// cell is of a different style.
        /// </summary>
        private void CreateSelectedLinkAdornments(TableRow row)
        {
            LinkButton selectedLink = GetSelectedLink();
            if (selectedLink != null)
            {
                foreach (TableCell cell in row.Cells)
                {
                    if ((cell.Controls.Count == 1) && (cell.Controls[0] == selectedLink))
                    {
                        cell.CssClass = "SubModuleNavigationSelectedSummary";

                        TableCell adornmentCell = new TableCell();
                        adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
                        row.Cells.AddAt(row.Cells.GetCellIndex(cell), adornmentCell);

                        const string prefixImage = "../Images/Row2_Left.gif";
                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(prefixImage));
                        Image image = new Image();
                        image.ImageUrl = prefixImage;
                        adornmentCell.Width = bitmap.Width;
                        adornmentCell.Controls.Add(image);

                        adornmentCell = new TableCell();
                        adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
                        row.Cells.AddAt(row.Cells.GetCellIndex(cell) + 1, adornmentCell);

                        const string suffixImage = "../Images/Row2_Right.gif";
                        bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(suffixImage));
                        image = new System.Web.UI.WebControls.Image();
                        image.ImageUrl = suffixImage;
                        adornmentCell.Width = bitmap.Width;
                        adornmentCell.Controls.Add(image);

                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Retrieve the name of the link that was last clicked on.
        /// </summary>
        /// <returns>Reference to the link button corresponding to the clicked link</returns>
        protected override LinkButton GetSelectedLink()
        {
            // Anytime a link is clicked on, the OnBubbleEvent handler stores the name of the link 
            // in the session. Retrieve that name and return the corresponding link button reference.

            object o = Page.Session[SelectedSubNavigationLinkKey];
            if (o != null)
            {
                string commandName = (string)o;
                if (null != disclosureLookupLinkButton)
                {
                    if (commandName == disclosureLookupLinkButton.CommandName)
                    {
                        return disclosureLookupLinkButton;
                    }
                }
                if (null != supervisorSummaryLinkButton)
                {
                    if (commandName == supervisorSummaryLinkButton.CommandName)
                    {
                        return supervisorSummaryLinkButton;
                    }
                }
                if (null != hierarchyMaintenanceLinkButton)
                {
                    if (commandName == hierarchyMaintenanceLinkButton.CommandName)
                    {
                        return hierarchyMaintenanceLinkButton;
                    }
                }
                if (commandName == myCoILinkButton.CommandName)
                {
                    return myCoILinkButton;
                }
                if (null != formsManagementLinkButton)
                {
                    if (commandName == formsManagementLinkButton.CommandName)
                    {
                        return formsManagementLinkButton;
                    }
                }
                if (null != standardReportLinkButton)
                {
                    if (commandName == standardReportLinkButton.CommandName)
                        return standardReportLinkButton;
                }
                if (null != customReportsLinkButton)
                {
                    if (commandName == customReportsLinkButton.CommandName)
                        return customReportsLinkButton;
                }
                if (null != cOIAdminLinkButton)
                {
                    if (commandName == cOIAdminLinkButton.CommandName)
                    {
                        return cOIAdminLinkButton;
                    }
                }
            }
            return myCoILinkButton;
        }

        /// <summary>
        /// 
        /// </summary>
        protected override string SelectedSubNavigationLinkKey
        {
            get
            {
                return selectedCoISubNavigationLinkKey;
            }
        }

        /// <summary>
        /// Create all controls in the HumansSubNavigation.
        /// </summary>
        protected override void CreateControls()
        {
            SetRequiredControls();
            Controls.Clear();

            myCoILinkButton = new LinkButton();
            myCoILinkButton.CommandName = myCoILinkButton.Text = "My COI";
            myCoILinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.COIHistoryURL", "");

            if (showDisclosureLookup)
            {
                disclosureLookupLinkButton = new LinkButton();
                disclosureLookupLinkButton.CommandName = disclosureLookupLinkButton.Text = DisclosureLookUp;
                disclosureLookupLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.COI.DisclosureLookupURL", "");
                disclosureLookupLinkButton.CausesValidation = false;
            }
            if (showSupervisorSummary)
            {
                supervisorSummaryLinkButton = new LinkButton();
                supervisorSummaryLinkButton.CommandName = supervisorSummaryLinkButton.Text = SupervisorSummary;
                supervisorSummaryLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.SupervisorSummaryURL", "");
                supervisorSummaryLinkButton.CausesValidation = false;
            }
            if (showHierarchyMaintenance)
            {
                hierarchyMaintenanceLinkButton = new LinkButton();
                hierarchyMaintenanceLinkButton.CommandName = hierarchyMaintenanceLinkButton.Text = HierarchyMaintenence;
                hierarchyMaintenanceLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.HierarchyMaintenanceURL", "");
                hierarchyMaintenanceLinkButton.CausesValidation = false;
            }
            if (showStandardReports)
            {
                standardReportLinkButton = new LinkButton();
                standardReportLinkButton.CommandName = standardReportLinkButton.Text = StandardReports;
                standardReportLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.StandardReportURL", "");
                standardReportLinkButton.CausesValidation = false;
            }
            if (showCustomReports)
            {
                customReportsLinkButton = new LinkButton();
                customReportsLinkButton.CommandName = customReportsLinkButton.Text = CustomReports;
                customReportsLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.CustomReportURL", "");
                customReportsLinkButton.CausesValidation = false;
            }

            if (showFormsManagement)
            {
                formsManagementLinkButton = new LinkButton();
                formsManagementLinkButton.CommandName = formsManagementLinkButton.Text = FormsManagement;
                formsManagementLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.FormsManagementURL", "");
                formsManagementLinkButton.CausesValidation = false;
            }
            if (showAdminManagement)
            {
                cOIAdminLinkButton = new LinkButton();
                cOIAdminLinkButton.CommandName = cOIAdminLinkButton.Text = AdminManagement;
                cOIAdminLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.CoI.DataManagementURL", "");
                cOIAdminLinkButton.CausesValidation = false;
            }
        }
        /// <summary>
        /// Set the selected link so that on load, the appropriate tab may be shown highlighted. This property is invoked
        /// by pages while vectoring to other pages which host this navigation control.
        /// </summary>
        public string SelectedLink
        {
            set { Page.Session[SelectedSubNavigationLinkKey] = value; }
        }
        #endregion

        #region Behavior Control properties
        /// <summary>
        /// Gets/Sets the AccesLevel of the User
        /// </summary>
        public int AccessLevel
        {
            get { return accessLevel; }
            set { accessLevel = value; }
        }

        /// <summary>
        /// Gets/Sets the AccesLevel of the User
        /// </summary>
        public bool IsLoggedInUserADelegate
        {
            get { return isLoggedInUserADelegate; }
            set { isLoggedInUserADelegate = value; }
        }
        #endregion

        #region Private methods
        /// <summary>
        /// Sets the required controls(links)which needs to be available depending on the access rights available to the logged in user.
        /// </summary>
        private void SetRequiredControls()
        {
            if ((accessLevel & (int)Security.Enumerations.ModuleRoleBit.SuperUser) > 0)  //User is SUPER USER
            {
                showCustomReports = true;
                showFormsManagement = true;
                showAdminManagement = true;
                //Hiding the standard reports page (All the functionalities of this page had been transported to the custom report page)
                //Note:No matter whoever logs in, the standard report page will be unavailable.
                showStandardReports = false;
            }
            else if (((accessLevel & (int)Security.Enumerations.ModuleRoleBit.SuperUser) == 0) &&
                      ((accessLevel & (int)Security.Enumerations.ModuleRoleBit.Supervisor) == 0)) //User is neither SuperUser NOR Supervisor
            {
                if (isLoggedInUserADelegate)  //User is delegate for either SuperUser or Supervisor.
                {
                    showSupervisorSummary = true;
                    showHierarchyMaintenance = true;
                    showDisclosureLookup = true;//No need of this as its set to true by default.
                }
                else
                {
                    showSupervisorSummary = false;
                    showHierarchyMaintenance = false;
                    showStandardReports = false;
                    showDisclosureLookup = (bool)((accessLevel & (int)Security.Enumerations.ModuleRoleBit.DisclosureLookupUser) > 0);
                }
            }


        }
        #endregion
    }

    #region Class CoISubNavigationDesigner
    /// <summary>
    /// Designer associated with the control.
    /// </summary>
    public class CoISubNavigationDesigner : ControlDesigner
    {
        #region Overrides
        /// <summary>
        /// Force the control to create its child controls for display at design time.
        /// </summary>
        /// <returns>HTML representation of the control</returns>
        public override string GetDesignTimeHtml()
        {
            ((CoISubNavigation)Component).Design();
            return base.GetDesignTimeHtml();
        }
        #endregion
    }
    #endregion
}
